package com.itcg.springboot01change.controller;

import com.itcg.springboot01change.pojo.Page;
import com.itcg.springboot01change.pojo.Product;
import com.itcg.springboot01change.pojo.Result;
import com.itcg.springboot01change.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    // 单条件查询
    @RequestMapping("/getById/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        Product byId = productService.getById(id);
        return Result.success(byId);
    }

    // 多条件模糊查询
    @RequestMapping("/getByCondition")
    public Result getByCondition(@RequestBody Map<String, Object> params) {
        List<Product> condition = productService.getByCondition(params);
        return Result.success(condition);
    }

    // 查询全部
    @RequestMapping("/getAll")
    public Result getAll() {
        List<Product> all = productService.getAll();
        return Result.success(all);
    }

    // 新增
    @RequestMapping("/insert")
    public Result insert(@RequestBody Product product) {
        int insert = productService.insert(product);
        return Result.success("添加成功: " + insert + " 行数据");
    }

    // 批量新增
    @RequestMapping("/batchInsert")
    public Result batchInsert(@RequestBody List<Product> productList) {
        int insert = productService.batchInsert(productList);
        return Result.success("添加成功: " + insert + " 行数据");
    }

    // 更新
    @RequestMapping("/update")
    public Result update(@RequestBody Product product) {
        int insert = productService.update(product);
        return Result.success("修改成功: " + insert + " 行数据");
    }

    // 动态字段更新
    @RequestMapping("/dynamicUpdate")
    private Result dynamicUpdate(@RequestBody Map<String, Object> params) {
        int insert = productService.dynamicUpdate(params);
        return Result.success("修改成功: " + insert + " 行数据");
    }

    // 单个删除
    @RequestMapping("/delete/{id}")
    private Result delete(@PathVariable Integer id) {
        int insert = productService.delete(id);
        return Result.success("删除成功: " + insert + " 行数据");
    }

    // 批量删除
    @RequestMapping("/batchDelete")
    private Result batchDelete(@RequestBody List<Integer> ids) {
        int insert = productService.batchDelete(ids);
        return Result.success("删除成功: " + insert + " 行数据");
    }

    // 条件删除
    @RequestMapping("/deleteByCondition")
    private Result deleteByCondition(@RequestBody Map<String, Object> params) {
        int batchDelete = productService.deleteByCondition(params);
        return Result.success("删除成功: " + batchDelete + " 行数据");
    }

    @GetMapping("/page")
    public Result getProductPage(@RequestBody() Page page) {
        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", page.getPageNum());
        params.put("pageSize", page.getPageSize());

        List<Product> products = productService.getByPage(params);
        return Result.success(products);
    }


}
